//
//  MyTripOrdersCtrl.m
//  Traveling
//
//  Created by 曹秀云 on 15/6/5.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "MyTripOrdersCtrl.h"
#import "MineDataTool.h"
#import "TripItemCell.h"
#import "TripOrderResult.h"
#import "TripDetailCtrl.h"

@interface MyTripOrdersCtrl ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *tipLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *itemArray;
@end

@implementation MyTripOrdersCtrl

-(NSMutableArray *)itemArray{
    if (!_itemArray) {
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"我的行程"];
    self.tipLabel.layer.borderWidth  = 1.0f;
    self.tipLabel.layer.borderColor  = HEXColor(@"#ec9b2b").CGColor;
    self.tipLabel.layer.cornerRadius = 5.0f;
    self.tipLabel.layer.masksToBounds=YES;
    [self initWithTableView];
    [self.tableView headerBeginRefreshing];
   
}

-(void)initWithTableView{
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    [self.tableView addHeaderWithTarget:self action:@selector(headerRefresh)];
    [self.tableView addFooterWithTarget:self action:@selector(footerRefresh)];
}


-(void)headerRefresh{
    [self requestDataWithMaxId:0];
}

-(void)footerRefresh{
    NSInteger maxId = 0;
    if(self.itemArray.count){
        TripOrderResult *result  = [self.itemArray lastObject];
            maxId =  result.plan_id;
    }
    if(maxId > 0){
        maxId--;
    }
    
    [self requestDataWithMaxId:maxId ];
}

-(void)requestDataWithMaxId:(NSInteger)maxId {
    NSString *uid = [DefaultsHelper stringForKey:kUserId];
    if (ISBLANK(uid)) {
        [DialogUtils showTips:@"未登录"];
        return;
    }
    [MineDataTool getMyTripOrders:@"19" maxId:maxId success:^(TripOrderRes *result) {
         [Utils refreshComplete:self.tableView];
        if (result.isSucc) {
            if(maxId == 0){
                [self.itemArray removeAllObjects];
            }
            if (result.datas.count) {
                [self.itemArray addObjectsFromArray:result.datas];
                [self.tableView reloadData];
            }
    }else{
            [DialogUtils showTips:result.msg];
        }
    } failure:^(NSError *error) {
        [Utils refreshComplete:self.tableView];
        [DialogUtils showTips:@"加载失败"];
    }];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.itemArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TripItemCell *cell = [TripItemCell cellWithTableView:tableView];
    [cell initData:self.itemArray[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return TRIPITEMCELLHEIGHT;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.navigationController pushViewController: [[TripDetailCtrl alloc]initWithTripOrderResult:self.itemArray[indexPath.row]] animated:YES];
}

@end
